package util.delicious;

import java.io.IOException;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.LinkedList;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.WhitespaceAnalyzer;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.queryParser.ParseException;

import puppy.demo.Launcher;

import util.hashing.Sorting;
import util.io.FileInput;
import util.ir.Indexer;
import util.ir.QueryIndex;
import util.nlp.LanguageModel;
import util.string.StringAnalysis;

public class TagMapping implements util.ir.Constants {

	public static Hashtable<String, String> tag_dictionary = new Hashtable<String, String>();

	public static Hashtable<String, String> tag_categories = new Hashtable<String, String>();

	static public void loadTagCategories() {

		String path = "/media/sata/data/delicious/final_category_tags.csv";

		// Hashtable<String, Hashtable<String, Float>> tags_categories = new
		// Hashtable<String, Hashtable<String, Float>>();

		FileInput in = new FileInput(path);
		String line = in.readString().toLowerCase();

		while (line != null) {

			String t[] = line.toLowerCase().split("\t");

			if (t.length > 1) {

				if (!t[0].equals("") && !t[1].equals("")) {

					tag_categories.put(t[1], t[0]);
					
				}
			}

			line = in.readString();

		}

	}

	static public String getTopCategory(String tag) {

		if (tag_categories.containsKey(tag)) {

			return tag_categories.get(tag);

			
		} else {

			return null;

		}

	}

	public static void ini() {

		String path = "/home/sergio/projects/data/delicious/experiments/dmoz_del_approx1.txt";
		LanguageModel.generate_aspectModel(path);
		loadTagCategories();
		loadTagList();
	}

	public static Hashtable<String, LinkedList<String>> getTags(String query)
			throws CorruptIndexException, ParseException, IOException {

		Hashtable<String, LinkedList<String>> hash = new Hashtable<String, LinkedList<String>>();

		HashSet<String> set = LanguageModel.getContextTags(query, 10);

		Iterator<String> iter = set.iterator();
	
		HashSet<String> set2 = new HashSet<String>();
		
		
		
		while (iter.hasNext()) {

			String next = iter.next().toLowerCase();

			if (tag_dictionary.containsKey(next)
					&& !tag_dictionary.get(next).equals("")) {

				String suggestion = tag_dictionary.get(next);

				String category = getTopCategory(suggestion);

				
				
				System.out.println(category + "\t" + suggestion);
				if (category != null &&!set2.contains(suggestion) ) {
					set2.add(suggestion);
					if (hash.containsKey(category)) {

						LinkedList<String> list = hash.get(category);
						list.add(suggestion);
					} else {

						LinkedList<String> list = new LinkedList<String>();

						list.add(suggestion);

						hash.put(category, list);

					}
				}

			}

		}

		return hash;

	}
	
	
	static public void printBestCategory(){
		
		
		
		loadTagList();
		loadTagCategories();
		
		Enumeration<String> keys = tag_dictionary.keys();
		
		
		while(keys.hasMoreElements()){
			
			String key = keys.nextElement();
			
			if(!tag_dictionary.get(key).equals("")){
				
				System.out.println(key + "\t"+ tag_dictionary.get(key) + "\t" + getTopCategory(key));
				
				
			}
			
		}
		
		
	}

	static public void loadTagList() {

		String path = "/media/sata/data/delicious/final_tags_dictionary.csv";

		FileInput in = new FileInput(path);
		String line = in.readString().toLowerCase();

		while (line != null) {

			String t[] = line.toLowerCase().split("\t");

			if (t.length > 1) {

				tag_dictionary.put(t[0], t[1]);

			}

			line = in.readString();
		}

	}

	static Hashtable<String, String> mapWikiToCategories(String path, int pos)
			throws CorruptIndexException, ParseException, IOException {

		// String redirect = "/media/sata/data/dbpedia/redirects_en.nt";
		String redirect = "/project/data/dbpedia/redirects_en.nt";

		Hashtable<String, String> redirects = Indexer.loadSimpleHash(redirect);

		FileInput in = new FileInput(path);
		Hashtable<String, String> hash = new Hashtable<String, String>();
		String line = in.readString();
		int total = 0;
		int hits = 0;
		while (line != null) {

			String page = line.split("\\s+")[pos].trim();
			// page = Indexer.decodeString(page);

			if (redirects.containsKey(page)) {

				page = redirects.get(page);
			}

			String wiki = getWikiCategory(page);
			total++;
			if (wiki != null) {
				hits++;
				hash.put(line.trim(), wiki);
				System.out.println(page + "\t" + wiki);
			} else {

				// System.out.println(page + "\t" + redirects.get(page) );
			}

			line = in.readString();
		}

		System.out.println("Coverage: " + (double) hits / (double) total);
		return hash;

	}

	public static String getDBPediaType(String string)
			throws CorruptIndexException, ParseException, IOException {

		String type = "";

		String index = "/project/data/indexes/dbpedia1_types/";
		Hashtable<String, Float> hash = new Hashtable<String, Float>();
		WhitespaceAnalyzer analyzer = new WhitespaceAnalyzer();

		QueryIndex.indexSearch(string, analyzer, WIKIPEDIA_TITLE,
				WIKI_TYPES_RAW, index, hash, 5);

		if (hash != null && hash.size() > 0) {
			Iterator<String> results = Sorting.getTopN(hash, false, 1);

			return results.next();

		} else {

			return null;
		}

	}

	public static Hashtable<String, String> mapTagsToWIkiPages(String file)
			throws CorruptIndexException, ParseException, IOException {
		Hashtable<String, String> hash = new Hashtable<String, String>();

		FileInput in = new FileInput(file);

		String line = in.readString();
		int total = 0;
		int hits = 0;
		while (line != null) {

			String wiki = mapTagToWIkiPage(line.trim().toLowerCase());
			total++;
			if (wiki != null) {
				hits++;
				hash.put(line.trim(), wiki);
				System.out.println(line.trim() + "\t" + wiki);
			}

			line = in.readString();
		}

		System.out.println("Coverage: " + (double) hits / (double) total);
		return hash;

	}

	public static String mapTagToWIkiPage(String tag)
			throws CorruptIndexException, ParseException, IOException {

		WhitespaceAnalyzer analyzer = new WhitespaceAnalyzer();
		String index = "/project/data/indexes/dbpedia1/";

		Hashtable<String, Float> hash = new Hashtable<String, Float>();

		QueryIndex.indexSearch(tag.toLowerCase().replaceAll(" ", "_"),
				analyzer, WIKIPEDIA_LABEL_REDIRECT, WIKIPEDIA_TITLE, index,
				hash, 10);

		if (hash != null && hash.size() > 0) {
			Iterator<String> results = Sorting.getTopN(hash, false, 1);

			return results.next();

		} else {

			return null;
		}

	}

	/**
	 * Get wikipedia category of wikipage (title specified)
	 * 
	 * 
	 * @param string
	 * @return
	 * @throws CorruptIndexException
	 * @throws ParseException
	 * @throws IOException
	 */
	public static String getWikiCategory(String string)
			throws CorruptIndexException, ParseException, IOException {

		String type = "";

		String index = "/project/data/indexes/wiki_categories2/";
		Hashtable<String, Float> hash = new Hashtable<String, Float>();
		WhitespaceAnalyzer analyzer = new WhitespaceAnalyzer();
		QueryIndex.indexSearch(string, analyzer, WIKIPEDIA_TITLE,
				WIKIPEDIA_CATEGORY_RAW, index, hash, 5);

		if (hash != null && hash.size() > 0) {
			Iterator<String> results = Sorting.getTopN(hash, false, 1);

			return results.next();

		} else {

			return null;
		}

	}

	public static void fillDictionaries() throws CorruptIndexException,
			ParseException, IOException {

		String tags_path = "/home/sergio/projects/data/delicious/experiments/tags.txt";
		String index = "/media/sata/data/indexes/dbpedia1/";

		FileInput in = new FileInput(tags_path);

		String line = in.readString();

		while (line != null) {

			String query = StringAnalysis.tagNormalization(line);

			Hashtable<String, Float> hash = new Hashtable<String, Float>();

			WhitespaceAnalyzer analyzer = new WhitespaceAnalyzer();

			QueryIndex.indexSearch(query, analyzer, WIKIPEDIA_LABEL_REDIRECT,
					WIKI_TYPES_RAW, index, hash, 10);

			// list = util.hashing.Sorting..getTopN(hash, false, 10);

			line = in.readString();
		}

	}

	public static void filterByFrequency(String path, int threshold) {

		FileInput in = new FileInput(path);

		String line = in.readString();
		Hashtable<String, Integer> hash = new Hashtable<String, Integer>();
		while (line != null) {
			line = line.trim();

			String t[] = line.split("\\s+");

			Integer i = new Integer(t[0]);

			if (i > threshold && t.length > 1)
				System.out.println(t[1]);

			/*
			 * if (hash.containsKey(line)) {
			 * 
			 * Integer i = hash.get(line); i = i + 1;
			 * 
			 * hash.put(line, i); } else {
			 * 
			 * hash.put(line, new Integer(1)); }
			 */

			line = in.readString();
		}

		/*
		 * Enumeration<String> keys = hash.keys();
		 * 
		 * while (keys.hasMoreElements()) {
		 * 
		 * String key = keys.nextElement();
		 * 
		 * if (hash.get(key) > threshold) {
		 * 
		 * System.out.println(key); }
		 * 
		 * }
		 */

	}

	public static void main(String arg[]) throws CorruptIndexException,
			ParseException, IOException {
		/*
		 * String wiki_path = ""; WhitespaceAnalyzer analyzer = new
		 * WhitespaceAnalyzer(); String index =
		 * "/project/data/indexes/dbpedia1/";
		 * 
		 * Hashtable<String, Float> hash = new Hashtable<String, Float>();
		 * 
		 * String kids_tags= "/project/data/delicious/tag_kids_filtered.txt";
		 * String tags =
		 * "/home/sergio/projects/data/delicious/experiments/dmoz_tags_frequency.txt"
		 * ;
		 * 
		 * 
		 * 
		 * filterByFrequency(tags,10);
		 */
		
		printBestCategory();
		/*TagMapping.ini();

		Hashtable<String, LinkedList<String>> suggestions = new Hashtable<String, LinkedList<String>>();

		try {
			suggestions = TagMapping.getTags("query");
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		// ini();
		getTags("tales");*/

		// mapTagsToWIkiPages(kids_tags);

		// getDBPediaType("Elmo");
		// System.out.println(mapTagToWIkiPages("sesamestreet"));

		// String tags_wikis= "/project/data/delicious/tags_mapping.txt";
		// mapWikiToCategories(tags_wikis,1);

		// System.out.println(getDBPediaType(""));

	}

}
